package com.huarun.common.util;

import java.util.concurrent.Executor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author hqy
 * @创建时间   2018-09-12 16:29
 * @Description 线程池
 */
@Configuration
@EnableAsync
public class ExecutorConfig {

	@Value("${threadpool.core-pool-size}")
	private int corePoolSize;

	@Value("${threadpool.max-pool-size}")
	private int maxPoolSize;

	@Value("${threadpool.queue-capacity}")
	private int queueCapacity;

	@Value("${threadpool.keep-alive-seconds}")
	private int keepAliveSeconds;

	@Bean
	public Executor taskExecutor() {
		ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
		pool.setKeepAliveSeconds(keepAliveSeconds);
		pool.setCorePoolSize(corePoolSize);//核心线程池数
		pool.setMaxPoolSize(maxPoolSize); // 最大线程
		pool.setQueueCapacity(queueCapacity);//队列容量
		pool.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy()); //队列满，线程被拒绝执行策略
		return pool;
	}
}
